import store from '@/store';
import {
	isWechat
} from '@/config/h5Utils';

// 支付(app、小程序)
function setPay(payInfo, callback) {
	let httpData = {
		orderId: payInfo.orderId,
		payType: payInfo.type
	}
	var url = '/approve/members/saveOrderPay' // 您的支付接口
	uni.$u.http.post(url, httpData).then((data) => {
		let payData = {
			success: function(res) {
				var flag = true
				// #ifdef MP-ALIPAY
				if (res.resultCode == 9000) {
					flag = true
				} else {
					flag = false
				}
				// #endif
				callback && callback({
					success: flag,
					data: res
				});
				console.log('success:' + JSON.stringify(res));
			},
			fail: function(err) {
				callback && callback({
					success: false,
					data: err
				});
				console.log('fail:' + JSON.stringify(err));
			}
		};
		if (payInfo.type == 2) {
			// 微信小程序
			payData.provider = 'wxpay';
			payData.timeStamp = data.timeStamp;
			payData.nonceStr = data.nonceStr;
			payData.package = data.packageValue;
			payData.signType = data.signType;
			payData.paySign = data.paySign;
		} else if (payInfo.type == 4) {
			// app微信
			payData.provider = 'wxpay';
			payData.orderInfo = payInfo.orderId;
		}
		console.log("支付参数", payData);
		uni.requestPayment(payData);
	}, err => {
		callback && callback({
			success: false,
			data: err
		});
	});
}

//公众号微信支付
function wxPublicPay(payInfo, callback) {
	if (!isWechat()) {
		uni.showToast({
			title: "需微信公众号才能使用~",
			icon: "none"
		});
		return
	}
	var url = '/approve/members/saveOrderPay'
	uni.$u.http.post(url, {
		orderId: payInfo.orderId,
		payType: payInfo.type
	}).then(data => {
		let wxConfigObj = {
			appId: data.appId,
			timeStamp: data.timeStamp,
			nonceStr: data.nonceStr,
			package: data.packageValue,
			signType: data.signType,
			paySign: data.paySign
		};

		function onBridgeReady() {
			window.WeixinJSBridge.invoke("getBrandWCPayRequest", wxConfigObj, function(
				res
			) {
				if (res.err_msg == "get_brand_wcpay_request:ok") {
					callback && callback({
						success: true,
						data: res
					});
				} else // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
					if (res.err_msg == "get_brand_wcpay_request:cancel") {
						// 您取消了支付
						callback && callback({
							success: false,
							data: res
						});
					} else
				if (res.err_msg == "get_brand_wcpay_request:fail") {
					// 支付失败
					callback && callback({
						success: false,
						data: res
					});
				}
			});
		}
		if (typeof window.WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener("WeixinJSBridgeReady", onBridgeReady, false);
			} else if (document.attachEvent) {
				document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
				document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
			}
		} else {
			onBridgeReady();
		}
	});
};

// 钱包支付
function walletPay(payInfo, callback) {
	let httpData = {
		orderId: payInfo.orderId,
		payType: 9
	}
	var url = '/approve/members/saveOrderPay' // 您的支付接口
	uni.$u.http.post(url, httpData).then((data) => {

		callback && callback({
			success: true,
			data: data
		});
	}, err => {
		callback && callback({
			success: false,
			data: err
		});
	});
}

export {
	setPay, //支付(app、小程序)
	wxPublicPay, //公众号微信支付
	walletPay,//钱包支付
}
